package com.zhongxi.guoyuan.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.zhongxi.guoyuan.OffLineShop.OffLineShopFragment;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.fragment.ExchangeHomeFragment;
import com.zhongxi.guoyuan.fragment.MainPersonFragment;
import com.zhongxi.guoyuan.fragment.MainRewardCoinFragment;
import com.zhongxi.guoyuan.fragment.NewPersonGiftFragment;
import com.zhongxi.guoyuan.fragment.beautifulStoreOrder.StoreOrderHasNoApplyFragment;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.beautifulStore.ProjectDetailActivity;
import com.zhongxi.guoyuan.view.user.LoginActivity;

public class MainActivity extends BaseActivity {

    private Fragment[] fragments;

    public MainRewardCoinFragment secondFragment;
    public OffLineShopFragment thirdFragment;
    public ExchangeHomeFragment fourFragment;
    public MainPersonFragment sixFragment;
    public int currentIndex = 0, index = -1;
    public Button[] tabs;
    public static final int REQUEST_CODE = 100;
    private NewPersonGiftFragment fiveFragment;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_main);
        tabs = new Button[6];
        tabs[0] = (Button) findViewById(R.id.tab0);
        tabs[1] = (Button) findViewById(R.id.tab1);
        tabs[2] = (Button) findViewById(R.id.tab2);
        tabs[3] = (Button) findViewById(R.id.tab3);
        tabs[4] = (Button) findViewById(R.id.tab4);
        tabs[5] = (Button) findViewById(R.id.tab5);

        secondFragment = new MainRewardCoinFragment();
        thirdFragment = new OffLineShopFragment();
        fourFragment = new ExchangeHomeFragment();
        fiveFragment = new NewPersonGiftFragment();
        sixFragment = new MainPersonFragment();

        fragments = new Fragment[]{secondFragment, secondFragment, thirdFragment, fourFragment, fiveFragment, sixFragment};
        index = getIntent().getIntExtra("index", -1);

        String mPaysuccess = getIntent().getStringExtra("entorypay");
        if (!TextUtils.isEmpty(mPaysuccess)) {
            Bundle mBundle = new Bundle();
            mBundle.putString("paysuccess", mPaysuccess);
            fourFragment.setArguments(mBundle);
        }
        if (index == 0) {
            startAct(MainHomeActivity.class);
            tabs[0].setSelected(true);
        } else {
            refresh();
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    public void refresh() {//1,2,3,4,5
        if (index != currentIndex) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            if (fragments[currentIndex].isAdded()) {
                trx.hide(fragments[currentIndex]);
            }
            if (!fragments[index].isAdded()) {
                trx.add(R.id.relaContainer, fragments[index]);
            }
            trx.show(fragments[index]).commit();
            tabs[index].setSelected(true);
            tabs[currentIndex].setSelected(false);
            currentIndex = index;
        }
        tabs[0].setSelected(false);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tab0:
                startAct(MainHomeActivity.class);
                setSelectFalse();
                tabs[0].setSelected(true);
                break;
            case R.id.tab1:
                index = 1;
                refresh();
                break;
            case R.id.tab2:
                index = 2;
                refresh();
                break;
            case R.id.tab3:
                index = 3;
                refresh();
                break;
            case R.id.tab4:
                showShortToast(UiUtils.getString(R.string.noline));
//                if (null != MyApplication.getInstance().getUserId() && !"".equals(MyApplication.getInstance().getUserId())) {
//                    index = 4;
//                    refresh();
//                } else {
//                    startAct(LoginActivity.class);
//                }
                break;
            case R.id.tab5:
                if (null != MyApplication.getInstance().getUserId() && !"".equals(MyApplication.getInstance().getUserId())) {
                    index = 5;
                    refresh();
                } else {
                    startAct(LoginActivity.class);
                }

                break;
        }
    }

    private void setSelectFalse() {
        for (int mI = 0; mI < 5; mI++) {
            tabs[mI].setSelected(false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == ProjectDetailActivity.RESULT_CODE) {
            index = 1;
            refresh();
        } else if (resultCode == StoreOrderHasNoApplyFragment.RESULTCODE) {
            index = 1;
            refresh();
        }
    }

    @Override
    public void onBackPressed() {
        setResult(2000);
        super.onBackPressed();
    }
}
